package org.jeecg.common.util.sms;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dypnsapi20170525.Client;
import com.aliyun.dypnsapi20170525.models.*;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AliYunUtil {
    private static String accessKeyId;
    private static String accessKeySecret;
    private static String endPoint;
    public static String getAccessKeyId() {
        return accessKeyId;
    }

    public static void setAccessKeyId(String accessKeyId) {
        AliYunUtil.accessKeyId = accessKeyId;
    }

    public static String getAccessKeySecret() {
        return accessKeySecret;
    }

    public static void setAccessKeySecret(String accessKeySecret) {
        AliYunUtil.accessKeySecret = accessKeySecret;
    }

    public static String getEndPoint() {
        return endPoint;
    }

    public static void setEndPoint(String endPoint) {
        AliYunUtil.endPoint = endPoint;
    }

    /**
     * 初始化客户端
     *
     * @return
     */
    private static Client initClient(String accessKeyId, String accessKeySecret, String endpoint) throws Exception {
        Config config = new Config().setAccessKeyId(accessKeyId).setAccessKeySecret(accessKeySecret).setEndpoint(endpoint);
        return new Client(config);
    }

    /**
     * 获取授权Token用于H5本机号码校验的鉴权，返回结果包括AccessToken和JwtToken
     */
    public static GetAuthTokenResponse getAuthToken(GetAuthTokenRequest getAuthTokenRequest) throws Exception {
        Client client = initClient(accessKeyId, accessKeySecret, endPoint);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            log.info("authTokenRequest============{}", JSONObject.toJSONString(getAuthTokenRequest));
            // 复制代码运行请自行打印 API 的返回值
            GetAuthTokenResponse response = client.getAuthTokenWithOptions(getAuthTokenRequest, runtime);
            log.info("authTokenResponse === {}", new Gson().toJson(response));
            return response;
        } catch (TeaException error) {
            error.printStackTrace();
        }
        return null;
    }

    /**
     * 一键登陆取号（H5能力专用） , 成功取得号码后会将号码返回
     */
    public static GetPhoneWithTokenResponse getPhoneWithToken(GetPhoneWithTokenRequest getPhoneWithTokenRequest) throws Exception{
        Client client = initClient(accessKeyId, accessKeySecret, endPoint);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            log.info("getPhoneWithToken============{}", JSONObject.toJSONString(getPhoneWithTokenRequest));
            GetPhoneWithTokenResponse response = client.getPhoneWithTokenWithOptions(getPhoneWithTokenRequest, runtime);
            log.info("getPhoneWithToken === {}", new Gson().toJson(response));
            return response;
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 本机号码验证（H5专用),认证完成后返回认证结果
     */
    public static VerifyPhoneWithTokenResponse verifyPhoneWithToken(VerifyPhoneWithTokenRequest verifyPhoneWithTokenRequest) throws Exception{
        Client client = initClient(accessKeyId, accessKeySecret, endPoint);
        try {
            log.info("verifyPhoneWithTokenRequest============{}", JSONObject.toJSONString(verifyPhoneWithTokenRequest));
            RuntimeOptions runtime = new RuntimeOptions();
            VerifyPhoneWithTokenResponse response = client.verifyPhoneWithTokenWithOptions(verifyPhoneWithTokenRequest, runtime);
            log.info("verifyPhoneWithToken === {}", new Gson().toJson(response));
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
